


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: IntrospectorUtils</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.util</a> ]
</div>

<h1>Coverage Summary for Class: IntrospectorUtils (at.dormancy.util)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">IntrospectorUtils</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (8/ 8)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    93.1%
  </span>
  <span class="absValue">
    (27/ 29)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.util;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import org.springframework.test.util.ReflectionTestUtils;
<i>19</i>&nbsp;import org.springframework.util.ReflectionUtils;
<i>20</i>&nbsp;
<i>21</i>&nbsp;import javax.annotation.Nonnull;
<i>22</i>&nbsp;import javax.annotation.Nullable;
<i>23</i>&nbsp;import java.beans.BeanInfo;
<i>24</i>&nbsp;import java.beans.IntrospectionException;
<i>25</i>&nbsp;import java.beans.Introspector;
<i>26</i>&nbsp;import java.beans.PropertyDescriptor;
<i>27</i>&nbsp;import java.lang.reflect.Method;
<i>28</i>&nbsp;import java.util.HashMap;
<i>29</i>&nbsp;import java.util.IdentityHashMap;
<i>30</i>&nbsp;import java.util.Map;
<i>31</i>&nbsp;
<i>32</i>&nbsp;/**
<i>33</i>&nbsp; * Utility class that simplifies bean introspection and caches {@link PropertyDescriptor}s for improved performance.&lt;/p&gt;
<i>34</i>&nbsp; * &lt;p/&gt;
<i>35</i>&nbsp; * There are some alternative solutions e.g.,
<i>36</i>&nbsp; * &lt;ul&gt;
<i>37</i>&nbsp; * &lt;li&gt;Spring´s {@link org.springframework.beans.CachedIntrospectionResults CachedIntrospectionResults} that is a
<i>38</i>&nbsp; * little bit slower but provides more features. Note that it is NOT intended for direct use by application code.&lt;/li&gt;
<i>39</i>&nbsp; * &lt;li&gt;Apache´s {@link org.apache.commons.beanutils.PropertyUtils PropertyUtils}, which are comparable to
<i>40</i>&nbsp; * {@code CachedIntrospectionResults}&lt;/li&gt;
<i>41</i>&nbsp; * &lt;li&gt;Spring´s {@link org.springframework.beans.ConfigurablePropertyAccessor ConfigurablePropertyAccessor} and
<i>42</i>&nbsp; * {@link org.springframework.beans.BeanWrapper BeanWrapper} do not use {@link PropertyDescriptor}s but also provide a
<i>43</i>&nbsp; * convenient way for accessing bean properties&lt;/li&gt;
<i>44</i>&nbsp; * &lt;/ul&gt;
<i>45</i>&nbsp; *
<i>46</i>&nbsp; * @author Gregor Schauer
<i>47</i>&nbsp; * @since 1.1.0
<i>48</i>&nbsp; */
<i>49</i>&nbsp;public class IntrospectorUtils {
<b class="fc"><i>50</i>&nbsp;	protected static Map&lt;Class&lt;?&gt;, Map&lt;String, PropertyDescriptor&gt;&gt; beanInfoMap</b>
<i>51</i>&nbsp;			= new IdentityHashMap&lt;Class&lt;?&gt;, Map&lt;String, PropertyDescriptor&gt;&gt;();
<i>52</i>&nbsp;
<b class="fc"><i>53</i>&nbsp;	private IntrospectorUtils() {</b>
<b class="fc"><i>54</i>&nbsp;	}</b>
<i>55</i>&nbsp;
<i>56</i>&nbsp;	/**
<i>57</i>&nbsp;	 * Returns the value of the specified property of the specified bean.
<i>58</i>&nbsp;	 *
<i>59</i>&nbsp;	 * @param bean         bean whose property is to be extracted
<i>60</i>&nbsp;	 * @param propertyName propertyName the propertyName of the property to get the value of
<i>61</i>&nbsp;	 * @return the value of the property
<i>62</i>&nbsp;	 * @see #getFieldValue(Object, String)
<i>63</i>&nbsp;	 * @see java.beans.PropertyDescriptor#getReadMethod()
<i>64</i>&nbsp;	 */
<i>65</i>&nbsp;	@Nullable
<i>66</i>&nbsp;	public static Object getValue(@Nonnull Object bean, @Nonnull String propertyName) {
<b class="fc"><i>67</i>&nbsp;		PropertyDescriptor descriptor = getDescriptorMap(bean.getClass()).get(propertyName);</b>
<b class="fc"><i>68</i>&nbsp;		Method readMethod = descriptor != null ? descriptor.getReadMethod() : null;</b>
<b class="fc"><i>69</i>&nbsp;		if (readMethod != null) {</b>
<b class="fc"><i>70</i>&nbsp;			return ReflectionUtils.invokeMethod(readMethod, bean);</b>
<i>71</i>&nbsp;		} else {
<b class="fc"><i>72</i>&nbsp;			return getFieldValue(bean, propertyName);</b>
<i>73</i>&nbsp;		}
<i>74</i>&nbsp;	}
<i>75</i>&nbsp;
<i>76</i>&nbsp;	/**
<i>77</i>&nbsp;	 * Returns the value of the specified property of the specified bean by accessing it directly.
<i>78</i>&nbsp;	 *
<i>79</i>&nbsp;	 * @param bean         bean whose property is to be extracted
<i>80</i>&nbsp;	 * @param propertyName the propertyName of the property to get the value of
<i>81</i>&nbsp;	 * @return the value of the property
<i>82</i>&nbsp;	 * @see ReflectionTestUtils#getField(Object, String)
<i>83</i>&nbsp;	 */
<i>84</i>&nbsp;	@Nonnull
<i>85</i>&nbsp;	protected static Object getFieldValue(@Nonnull Object bean, @Nonnull String propertyName) {
<b class="fc"><i>86</i>&nbsp;		return ReflectionTestUtils.getField(bean, propertyName);</b>
<i>87</i>&nbsp;	}
<i>88</i>&nbsp;
<i>89</i>&nbsp;	/**
<i>90</i>&nbsp;	 * Sets the value of the specified property of the specified bean.
<i>91</i>&nbsp;	 *
<i>92</i>&nbsp;	 * @param bean         bean whose property is to be modified
<i>93</i>&nbsp;	 * @param propertyName propertyName of the property to be modified
<i>94</i>&nbsp;	 * @param value        value to which this property is to be set
<i>95</i>&nbsp;	 * @see #setFieldValue(Object, String, Object)
<i>96</i>&nbsp;	 * @see java.beans.PropertyDescriptor#getWriteMethod()
<i>97</i>&nbsp;	 */
<i>98</i>&nbsp;	public static void setValue(@Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value) {
<b class="fc"><i>99</i>&nbsp;		PropertyDescriptor descriptor = getDescriptorMap(bean.getClass()).get(propertyName);</b>
<b class="fc"><i>100</i>&nbsp;		Method writeMethod = descriptor != null ? descriptor.getWriteMethod() : null;</b>
<b class="fc"><i>101</i>&nbsp;		if (writeMethod != null) {</b>
<b class="fc"><i>102</i>&nbsp;			ReflectionUtils.invokeMethod(writeMethod, bean, value);</b>
<i>103</i>&nbsp;		} else {
<b class="fc"><i>104</i>&nbsp;			setFieldValue(bean, propertyName, value);</b>
<i>105</i>&nbsp;		}
<b class="fc"><i>106</i>&nbsp;	}</b>
<i>107</i>&nbsp;
<i>108</i>&nbsp;	/**
<i>109</i>&nbsp;	 * Sets the value of the specified property of the specified bean.
<i>110</i>&nbsp;	 *
<i>111</i>&nbsp;	 * @param bean         bean whose property is to be set
<i>112</i>&nbsp;	 * @param propertyName the propertyName of the property to set the value of
<i>113</i>&nbsp;	 * @param value        the new value
<i>114</i>&nbsp;	 * @see ReflectionTestUtils#setField(Object, String, Object)
<i>115</i>&nbsp;	 */
<i>116</i>&nbsp;	protected static void setFieldValue(@Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value) {
<b class="fc"><i>117</i>&nbsp;		ReflectionTestUtils.setField(bean, propertyName, value);</b>
<b class="fc"><i>118</i>&nbsp;	}</b>
<i>119</i>&nbsp;
<i>120</i>&nbsp;	/**
<i>121</i>&nbsp;	 * Returns a map containing the property names and their corresponding property descriptors.&lt;p/&gt;
<i>122</i>&nbsp;	 * &lt;p/&gt;
<i>123</i>&nbsp;	 * Due to the fact that {@link #getClass()} is a valid getter method, the method
<i>124</i>&nbsp;	 * {@link java.beans.BeanInfo#getPropertyDescriptors()} returns a property descriptor for {@code class}.
<i>125</i>&nbsp;	 * This method does NOT return the property {@code class}.
<i>126</i>&nbsp;	 *
<i>127</i>&nbsp;	 * @param clazz the class to retrieve its property descriptors
<i>128</i>&nbsp;	 * @return the property descriptors of the class
<i>129</i>&nbsp;	 * @see #getDescriptors(Class)
<i>130</i>&nbsp;	 */
<i>131</i>&nbsp;	@Nonnull
<i>132</i>&nbsp;	protected static Map&lt;String, PropertyDescriptor&gt; getDescriptorMap(@Nonnull Class&lt;?&gt; clazz) {
<b class="fc"><i>133</i>&nbsp;		Map&lt;String, PropertyDescriptor&gt; descriptorMap = beanInfoMap.get(clazz);</b>
<b class="fc"><i>134</i>&nbsp;		if (descriptorMap == null) {</b>
<b class="fc"><i>135</i>&nbsp;			descriptorMap = new HashMap&lt;String, PropertyDescriptor&gt;();</b>
<b class="fc"><i>136</i>&nbsp;			beanInfoMap.put(clazz, descriptorMap);</b>
<i>137</i>&nbsp;
<b class="fc"><i>138</i>&nbsp;			for (PropertyDescriptor descriptor : getDescriptors(clazz)) {</b>
<b class="fc"><i>139</i>&nbsp;				if (!&quot;class&quot;.equals(descriptor.getName())) {</b>
<b class="fc"><i>140</i>&nbsp;					descriptorMap.put(descriptor.getName(), descriptor);</b>
<i>141</i>&nbsp;				}
<i>142</i>&nbsp;			}
<i>143</i>&nbsp;		}
<b class="fc"><i>144</i>&nbsp;		return descriptorMap;</b>
<i>145</i>&nbsp;	}
<i>146</i>&nbsp;
<i>147</i>&nbsp;	/**
<i>148</i>&nbsp;	 * Returns the property descriptors of the given class.&lt;/p&gt;
<i>149</i>&nbsp;	 * &lt;p/&gt;
<i>150</i>&nbsp;	 * This is just a convenient method that catches the {@link IntrospectionException} and rethrows it as an unchecked
<i>151</i>&nbsp;	 * exception.
<i>152</i>&nbsp;	 *
<i>153</i>&nbsp;	 * @param clazz the class to retrieve its property descriptors
<i>154</i>&nbsp;	 * @return the property descriptors of the class
<i>155</i>&nbsp;	 * @see Introspector#getBeanInfo(Class)
<i>156</i>&nbsp;	 * @see java.beans.BeanInfo#getPropertyDescriptors()
<i>157</i>&nbsp;	 */
<i>158</i>&nbsp;	@Nonnull
<i>159</i>&nbsp;	private static PropertyDescriptor[] getDescriptors(@Nonnull Class&lt;?&gt; clazz) {
<i>160</i>&nbsp;		try {
<b class="fc"><i>161</i>&nbsp;			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);</b>
<b class="fc"><i>162</i>&nbsp;			return beanInfo.getPropertyDescriptors();</b>
<b class="nc"><i>163</i>&nbsp;		} catch (IntrospectionException e) {</b>
<b class="nc"><i>164</i>&nbsp;			throw new RuntimeException(e);</b>
<i>165</i>&nbsp;		}
<i>166</i>&nbsp;	}
<i>167</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
